package com.controller.dictionary;


import com.common.ResultUtil;
import com.common.WebContextHelper;
import com.common.stereotype.Token;
import com.controller.base.BaseLocalController;
import com.entity.Manager;
import com.entity.dictionary.Department;
import com.facade.dictionary.dept.IDepartmentFacadeCUD;
import com.facade.dictionary.dept.IDepartmentFacadeR;
import com.query.DefaultQuery;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@Controller
@RequestMapping("/dept")
//@Module(parent = "系统模块", name = "部门管理", parentModuleOrder = 0, childModuleOrder = 4)
public class DepartmentController extends BaseLocalController {

    @Resource
    private IDepartmentFacadeR departmentFacadeR;
    @Resource
    private IDepartmentFacadeCUD departmentFacadeCUD;

    @ModelAttribute
    public void prepare(Long id, ModelMap modelMap) {
        HttpServletRequest request = WebContextHelper.getRequest();
        String requestUri = request.getRequestURI();
        if (requestUri.contains("modify") && null != id) {
            modelMap.addAttribute(departmentFacadeR.findByPrimaryKey(id));
        }
    }

    @RequestMapping(value = "/query.htm")
//    @Uri(name = "查询", uri = "/dept/query", order = 0)
    public String query(@ModelAttribute("query") DefaultQuery query, ModelMap modelMap) {
        modelMap.addAttribute("departmentList", departmentFacadeR.findRankAll());
        return "dictionary/department-List";
    }

    @RequestMapping(value = "/edit.htm")
    public String edit(Long id, ModelMap modelMap) {
        if (null != id) {
            modelMap.addAttribute(departmentFacadeR.findByPrimaryKey(id));
        }
        modelMap.addAttribute("pList", departmentFacadeR.findByPropertyEqualTo("pid", null, "seq ASC"));
        return "dictionary/department-edit";
    }

//    @Token
    @ResponseBody
    @RequestMapping(value = "/add.htm", method = RequestMethod.POST)
//    @Uri(name = "添加", uri = "/dept/add", order = 1)
    public ResultUtil add(@Valid Department department, BindingResult error) {
        ResultUtil resultUtil = validate(error);
        if (ResultUtil.SUCCESS != resultUtil.getStatus()) {
            return resultUtil;
        }
        Manager onLineUser = WebContextHelper.getOnLineUser();
        return departmentFacadeCUD.addDepartment(department, onLineUser.getId());
    }

//    @Token
    @ResponseBody
    @RequestMapping(value = "/modify.htm", method = RequestMethod.POST)
//    @Uri(name = "修改", uri = "/dept/modify", order = 2)
    public ResultUtil modify(@Valid Department department) {
        Manager onLineUser = WebContextHelper.getOnLineUser();
        return departmentFacadeCUD.modifyDepartment(department, onLineUser.getId());
    }

    @ResponseBody
    @RequestMapping(value = "/delete.htm/{id}")
//    @Uri(name = "删除", uri = "/dept/delete", order = 3)
    public ResultUtil delete(@PathVariable("id") Long id) {
        Manager onLineUser = WebContextHelper.getOnLineUser();
        return departmentFacadeCUD.deleteDepartment(id, onLineUser.getId());
    }

    @ResponseBody
    @RequestMapping(value = "/ajaxLoad.htm/{pid}")
    public ResultUtil queryByParent(@PathVariable("pid") Long pid) {
        ResultUtil resultUtil = ResultUtil.newInstance();
        resultUtil.setData(departmentFacadeR.findByPropertyEqualTo("pid", pid, "seq ASC"));
        return resultUtil;
    }

}